<?php

namespace App\Events;

use App\Message;
use App\User;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class MessagePosted implements ShouldBroadcast
{
    // SerializesModels trait 的事件将会优雅的序列化任何的 Eloquent 模型
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Message
     *
     * @var Message
     */
    public $message;

    /**
     * User
     *
     * @var User
     */
    public $user;

    public $update;

    /**
     * 指定事件被放置在哪个队列上
     *
     * @var string
     */
    //public $broadcastQueue = 'your-queue-name';

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Message $message, User $user)
    {
        $this->message = $message;
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     * Channel频道表示任意用户可以订阅公共频道，
     * PrivateChannels或PresenceChannels则代表需要进行频道授权的私有频道
     * 返回一个频道或一个频道数组
     *  频道必须是 Channel、PrivateChannel 或 PresenceChannel 的实例
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('chatroom');
        //return new PresenceChannel('chatroom');
        //return new Channel('everyone');

        //return new PrivateChannel('order.'.$this->update->order_id);

    }

    public function broadcastAs()
    {
        return 'server.created';
    }

    /**
     * 指定广播数据
     *
     * @return array
     */
    // public function broadcastWith()
    // {
    //     return ['user_id' => $this->body];
    // }
}
